package com.ada.base.common;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class DataObject implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -7086483524431025262L;
    private Map<String, Object> map = new HashMap<String, Object>();

    public Object getValue(String property) {
        return map.get(property);
    }

    public Object get(String property) {
        return map.get(property);
    }

    public int size() {
        return this.map.size();
    }

    public String getString(String property, String defaultValue) {
        if (map.containsKey(property) || map.get(property) != null)
            return String.valueOf(map.get(property));
        return defaultValue;
    }

    public String getString(String property) {
        return getString(property, "");
    }

    public int getInt(String property, int defaultValue) {
        if (!"".equals(getString(property)))
            return Integer.parseInt(getString(property));
        return defaultValue;
    }

    public int getInt(String property) {
        return getInt(property, 0);
    }

    public long getLong(String property, long defaultValue) {
        if (!"".equals(getString(property)))
            return Long.parseLong(getString(property));
        return defaultValue;
    }

    public long getLong(String property) {
        return getLong(property, 0l);
    }

    public double getDouble(String property, double defaultValue) {
        if (!"".equals(getString(property)))
            return Double.parseDouble(getString(property));
        return defaultValue;
    }

    public double getDouble(String property) {
        return getDouble(property, 0d);
    }

    public boolean getBoolean(String property, boolean defaultValue) {
        if (!"".equals(getString(property)))
            return Boolean.parseBoolean(getString(property));
        return defaultValue;
    }

    public boolean getBoolean(String property) {
        return getBoolean(property, false);
    }

    public DataObject getData(String property) {
        if (this.map.containsKey(property) && this.map.get(property) != null) {
            Object obj = this.map.get(property);
            if (obj instanceof DataObject)
                return (DataObject) obj;
        }
        return null;
    }

    public void setValue(String property, Object value) {
        map.put(property, value);
    }

    public void putAll(Map<String, Object> submap) {
        map.putAll(submap);
    }

    public void putAll(DataObject submap) {
        map.putAll(submap.getAll());
    }

    public Map<String, Object> getAll() {
        return map;
    }

    public String[] getNames() {
        String[] names = (String[]) this.map.keySet().toArray(new String[0]);
        Arrays.sort(names);
        return names;
    }

    public boolean containsKey(String property) {
        return this.map.containsKey(property);
    }

    public boolean isEmpty() {
        return this.map.isEmpty();
    }

    public void clear() {
        this.map.clear();
    }
    
    public void remove(Object value){
    	this.map.remove(value);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        String[] names = this.getNames();
        res.append("[");
        for (String name : names) {
            res.append(name);
            res.append(" = ");
            res.append(this.getString(name));
            res.append(", ");
        }
        res.append("]");
        return res.toString();
    }

}
